/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

/**
 *
 * @author Administrator
 */
public class ClipboardExtensions {

    public static String getText() {
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        String text = null;
        if (c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
            try {
                text = (String) c.getData(DataFlavor.stringFlavor);
            } catch (Exception ex) {
            }
        }
        return text;
    }

    public static void setText(final String text) {
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        c.setContents(new Transferable() {

            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[]{DataFlavor.stringFlavor};
            }

            public boolean isDataFlavorSupported(DataFlavor flavor) {
                return (flavor == DataFlavor.stringFlavor);
            }

            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
                if (flavor == DataFlavor.stringFlavor) {
                    return text;
                }
                throw new UnsupportedFlavorException(flavor);
            }
        }, null);
    }
}
